<?php
namespace DRY\Persistence;

/**
 * DonReY Framework 2012 :: Module component: Persistence Container Session
 *
 * Session storage container, saves data inside PHP's $_SESSION variable.
 * This is a non-shared container which doesn't need an owner ID.
 *
 * @author Wavetrex <wavetrex@gmail.com>
 * @link http://www.donrey.net/Documentation/Structure/Modules/persistence.html
 * @license [GPL v3] http://www.gnu.org/licenses/gpl-3.0.html
 * @version 1.0.dev
 * @package DonReY
 * @subpackage Module.Persistence
 */
class Container_Session extends Container_base
{
	/**
	 * Session Global init. Does not require configuration, there is only one type of session... the $_SESSION
	 * TODO: (Maybe) session_name allows setting a session name, use some config value to set that instead of the default one.
	 *
	 * @return bool true. Nothing that could go wrong here...
	 */
	static public function global_Init()
	{
		if(isset(self::$_initialized['session']))
			return true; // already performed global init on session storage engine

		// Work-around for setting up a session because Flash Player doesn't send the cookies ( needed for swfupload )
		if (isset($_POST["PHPSESSID"])) {
			session_id($_POST["PHPSESSID"]);
		}
		if(session_id()!=true)
			session_start();

		self::$_initialized['session']=true;
		return true;
	}

	/**
	 * Destructor for session container
	 * - nothing to do here, sessions are saved automatically by PHP
	 */
	public function __destruct()
	{
	}

	/**
	 * Implementation of __get for the session container
	 *
	 * @param string $varname
	 */
	public function &__get($varname)
	{
		// to avoid creating a new key in $_SESSION for the inexistent variable (when attempting to read it)
		if((isset($_SESSION[$this->_name]) && is_array($_SESSION[$this->_name]) && array_key_exists($varname, $_SESSION[$this->_name])) || $varname=="__expiry")
			return $_SESSION[$this->_name][$varname];
		else {
			$null = null;
			return $null;
		}
	}

	/**
	 * Implementation of __set for the session container
	 *
	 * @param string $varname
	 * @param mixed $value
	 */
	public function __set($varname, $value)
	{
		$_SESSION[$this->_name][$varname] = $value;
	}

	/**
	 * Implementation of __isset for the session container
	 *
	 * @param string $varname
	 * @return bool
	 */
	public function __isset($varname)
	{
		return isset($_SESSION[$this->_name]) && is_array($_SESSION[$this->_name]) && array_key_exists($varname, $_SESSION[$this->_name]);
	}

	/**
	 * Implementation of __unset for the session container
	 *
	 * @param string $varname
	 */
	public function __unset($varname)
	{
		unset($_SESSION[$this->_name][$varname]);
	}

	/**
	 * Implementation of template Countable for the session container
	 *
	 * @return int
	 */
	public function count()
	{
		return isset($_SESSION[$this->_name])?count($_SESSION[$this->_name]):0;
	}

	/**
	 * Implementation of destroy() for the session container
	 */
	public function destroy()
	{
		unset($_SESSION[$this->_name]);
	}

	/**
	 * Implementation of read_all() for the session container
	 *
	 * @return array
	 */
	public function &read_All()
	{
		if(isset($this->__expiry)) { // if expiry was implemented, you cannot return session var directly...
			$ret = $_SESSION[$this->_name]; // make a copy
			unset($ret['__expiry']); // remove the special variable ( actually PHP makes a copy only here... )
			return $ret;
		} else
			return $_SESSION[$this->_name];
	}

}
?>